/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet chargée de gérer toutes les exceptions non gérées
 * par les autres servlet.
 *
 * Définie dans web.xml pour gérer les Error 403 et 404
 *
 * @author Thomas
 * @version 1.0
 */
public class ErrorHandler extends HttpServlet {

    private RequestDispatcher dispatcher;
    private String error_msg;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");

            switch(statusCode) {
                case 404: error_msg = "La page demand&eacute;e n&apos;est pas disponible. <a href=\"Index\">Revenir &agrave la page d&apos;accueil.</a>";
                          break;
                case 403: error_msg = "L'acc&egrave;s &agrave; cette page n&apos;est pas autoris&eacute;. <a href=\"Index\">Revenir &agrave la page d&apos;accueil.</a>";
                          break;
                default:  error_msg = "D&eacute;sol&eacute;, notre site est temporairement indisponible.";
                          break;
            }

            request.setAttribute("error_msg", error_msg);
            dispatcher = request.getRequestDispatcher("_error.jsp");
        } finally {
            dispatcher.forward(request, response);
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
